<template>
  <el-dialog
    :title="!dataForm.areaalarmNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="区域编号" prop="areaId">
      <el-input v-model="dataForm.areaId" placeholder="区域编号"></el-input>
    </el-form-item>
    <el-form-item label="标签号" prop="lableId">
      <el-input v-model="dataForm.lableId" placeholder="标签号"></el-input>
    </el-form-item>
    <el-form-item label="人员编号" prop="lableEmployid">
      <el-input v-model="dataForm.lableEmployid" placeholder="人员编号"></el-input>
    </el-form-item>
    <el-form-item label="报警时间" prop="alarmTime">
      <el-input v-model="dataForm.alarmTime" placeholder="报警时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          areaalarmNo: 0,
          areaId: '',
          lableId: '',
          lableEmployid: '',
          alarmTime: ''
        },
        dataRule: {
          areaId: [
            { required: true, message: '区域编号不能为空', trigger: 'blur' }
          ],
          lableId: [
            { required: true, message: '标签号不能为空', trigger: 'blur' }
          ],
          lableEmployid: [
            { required: true, message: '人员编号不能为空', trigger: 'blur' }
          ],
          alarmTime: [
            { required: true, message: '报警时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.areaalarmNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.areaalarmNo) {
            this.$http({
              url: this.$http.adornUrl(`/alarmhistory/areaalarmhistory/info/${this.dataForm.areaalarmNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.areaId = data.areaalarmHistory.areaId
                this.dataForm.lableId = data.areaalarmHistory.lableId
                this.dataForm.lableEmployid = data.areaalarmHistory.lableEmployid
                this.dataForm.alarmTime = data.areaalarmHistory.alarmTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/alarmhistory/areaalarmhistory/${!this.dataForm.areaalarmNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'areaalarmNo': this.dataForm.areaalarmNo || undefined,
                'areaId': this.dataForm.areaId,
                'lableId': this.dataForm.lableId,
                'lableEmployid': this.dataForm.lableEmployid,
                'alarmTime': this.dataForm.alarmTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
